package com.shp.utils;

import com.shp.pojo.User;
import org.springframework.lang.Nullable;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Description:
 * 登录拦截器
 * @author zwl
 * @version 1.0
 * @date 2020/3/18 18:47
 */
public class LoginInterceptor implements HandlerInterceptor {

    /**
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) throws Exception {
    }

    /**
     * 该方法将在整个请求完成之后，也就是DispatcherServlet渲染了视图执行
     * 此处用于记录在线人数
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception {
        HttpSession session = request.getSession();
        OnlineUserUtil.add(session);
        User user = (User)session.getAttribute("activeUser");
        OnlineUserUtil.addUserEmail(user);
        System.out.println("++++++++++++++++++++++++++++++++++：Online" + OnlineUserUtil.getSize());
    }
}
